//给你一个二叉树的根节点 root ，按 任意顺序 ，返回所有从根节点到叶子节点的路径。 
//
// 叶子节点 是指没有子节点的节点。 
// 
//
// 示例 1： 
//
// 
//输入：root = [1,2,3,null,5]
//输出：["1->2->5","1->3"]
// 
//
// 示例 2： 
//
// 
//输入：root = [1]
//输出：["1"]
// 
//
// 
//
// 提示： 
//
// 
// 树中节点的数目在范围 [1, 100] 内 
// -100 <= Node.val <= 100 
// 
// Related Topics 树 深度优先搜索 字符串 二叉树 👍 623 👎 0

package leetcode.editor.cn;
//leetcode submit region begin(Prohibit modification and deletion)

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution257 {
    /**
     * 迭代法
     */
    public List<String> binaryTreePaths(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        List<String> result = new ArrayList<>();
        Stack<String> path = new Stack<>();
        if(root == null) return result;
        stack.push(root);
        path.push(root.val+"");
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            String pathSt = path.pop();
            if(node.left == null && node.right == null){
                result.add(pathSt);
            }
            if(node.right != null){
                stack.push(node.right);
                path.push(pathSt+"->"+node.right.val);
            }
            if(node.left != null){
                stack.push(node.left);
                path.push(pathSt+"->"+node.left.val);
            }
        }
        return result;
    }


        /**
         * 回溯
         */
    public List<String> binaryTreePaths1(TreeNode root) {
        List<String> list = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        if(root == null) return list;
        getPath(root,list,path);
        return list;
    }

    public void getPath(TreeNode root, List<String> list, List<Integer> paths){
        paths.add(root.val);
        if(root.left == null && root.right == null){
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < paths.size(); i++){
                if(i == paths.size()-1){
                    sb.append(paths.get(i));
                } else {
                    sb.append(paths.get(i)).append("->");
                }
            }
            list.add(sb.toString());
            return;
        }
        if(root.left != null){
            getPath(root.left,list,paths);
            paths.remove(paths.size()-1);
        }
        if(root.right != null){
            getPath(root.right,list,paths);
            paths.remove(paths.size()-1);        }
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.right = new TreeNode(5);
        root.right = new TreeNode(3);
        List<String> strings = new Solution257().binaryTreePaths(root);
        System.out.println(strings.size());
    }
}
//leetcode submit region end(Prohibit modification and deletion)
